package com.baikeyang.service.toilet.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.ToiletInfo;
import com.baikeyang.common.util.MapCommonUtil;
import com.baikeyang.common.util.RedisUtil;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.mapper.ToiletInfoMapper;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baikeyang.service.toilet.IToiletInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 洗手间详细信息 服务实现类
 * </p>
 *
 * @author Yangcq
 * @since 2019-04-01
 */
@Service
public class ToiletInfoServiceImpl extends SuperServiceImpl<ToiletInfoMapper, ToiletInfo> implements IToiletInfoService {

    @Resource
    ToiletInfoMapper toiletInfoMapper;

    @Override
    public ResponseResult getToiletsByAreaCode(String areaType, String areaCode) {
        QueryWrapper<ToiletInfo> toiletQuery = Wrappers.query();
        toiletQuery.eq(areaType, areaCode);
        List<ToiletInfo> toiletInfos = toiletInfoMapper.selectList(toiletQuery);
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, toiletInfos);
    }


    @Override
    public ResponseResult getToiletsByLnglat(String lng, String lat, String radius) {
        if(StringUtils.isNotEmpty(radius)){
            radius = "5000";
        }
        if(StringUtils.isEmpty(lng)){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.失败, ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_403);
        }
        if(StringUtils.isEmpty(lat)){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.失败, ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_403);
        }
        Double clng = Double.parseDouble(lng);
        Double clat = Double.parseDouble(lat);
        Double r = Double.parseDouble(radius);
        List<ToiletInfo> result = new ArrayList<>();

        QueryWrapper<ToiletInfo> toiletQuery = Wrappers.query();
        List<ToiletInfo> toiletInfos = toiletInfoMapper.selectList(toiletQuery);
        if(CollectionUtil.isNotEmpty(toiletInfos)){
            for (ToiletInfo info : toiletInfos) {
                // 判断车辆是否在区域内
                if (MapCommonUtil.isPointInCircleArea(Double.parseDouble(info.getBmapLng()), Double.parseDouble(info.getBmapLat()), clng, clat, r)) {
                    result.add(info);
                }
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, result);
    }

    @Override
    public ResponseResult getToilets() {
        QueryWrapper<ToiletInfo> toiletQuery = Wrappers.query();
        List<ToiletInfo> toiletInfos = toiletInfoMapper.selectList(toiletQuery);
        Jedis jedis = RedisDS.create().getJedis();
        Pipeline p = jedis.pipelined();
        if(CollectionUtil.isNotEmpty(toiletInfos)){
            List<String> key = new ArrayList<>();
            List<String> bValue = new ArrayList<>();

            List<String> qqValue = new ArrayList<>();
            for (ToiletInfo toiletInfo: toiletInfos) {
                key.add(toiletInfo.getId().toString());

                // 向存储写入百度经纬度
                String bLnglat = toiletInfo.getBmapLng()+","+toiletInfo.getBmapLat();
                bValue.add(bLnglat);
                //p.hset("toilet-position-bmap", toiletInfo.getId().toString(), bLnglat);
                // 向存储写入腾讯经纬度
                String qqLnglat = toiletInfo.getQqmapLng()+","+toiletInfo.getQqmapLat();
                qqValue.add(qqLnglat);
                //p.hset("toilet-position-qqmap", toiletInfo.getId().toString(), qqLnglat);
                // 向存储写入区域位置信息
                //jedis.hset("toilet-position-area", toiletInfo.getId().toString(), qqLnglat);
            }
            try {
                RedisUtil.setHSet("toilet-position-bmap", key, bValue);
                RedisUtil.setHSet("toilet-position-qqmap", key, qqValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, toiletInfos);
    }
}
